package com.java110.mall.goods.cmd.product;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.product.ProductPriceRuleDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.intf.goods.IProductPriceRuleV1InnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.po.product.ProductSpecValuePo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Java110Cmd(serviceCode = "product.updateProductPrice")
public class UpdateProductPriceCmd extends Cmd {

    @Autowired
    private IProductPriceRuleV1InnerServiceSMO productPriceRuleV1InnerServiceSMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "shopId", "未包含店铺");
        Assert.hasKeyAndValue(reqJson, "productIds", "未包含商品");
        Assert.hasKeyAndValue(reqJson, "ruleId", "未包含定价策略");
    }

    @Override
    @Java110Transactional
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        String[] productIds = reqJson.getString("productIds").split(",");
        if (productIds == null || productIds.length < 1) {
            throw new CmdException("定价商品为空");
        }

        ProductSpecValueDto productSpecValueDto = new ProductSpecValueDto();
        productSpecValueDto.setProductIds(productIds);
        productSpecValueDto.setShopId(reqJson.getString("shopId"));
        List<ProductSpecValueDto> productDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);

        if (productDtos == null || productDtos.size() < 1) {
            throw new CmdException("未查询到商品");
        }

        ProductPriceRuleDto productPriceRuleDto = new ProductPriceRuleDto();
        productPriceRuleDto.setRuleId(reqJson.getString("ruleId"));
        List<ProductPriceRuleDto> productPriceRuleDtos = productPriceRuleV1InnerServiceSMOImpl.queryProductPriceRules(productPriceRuleDto);

        Assert.listOnlyOne(productPriceRuleDtos, "未包含定价策略");
        productPriceRuleDtos.get(0).setProductSpecValueDtos(productDtos);
        List<ProductSpecValueDto> tmpProductSpecValueDtos = productPriceRuleV1InnerServiceSMOImpl.computeProductSpecValuePrices(productPriceRuleDtos.get(0));

        ProductSpecValuePo productSpecValuePo = null;
        int flag = 0;
        for (ProductSpecValueDto tmpProductSpecValueDto : tmpProductSpecValueDtos) {
            productSpecValuePo = BeanConvertUtil.covertBean(tmpProductSpecValueDto, ProductSpecValuePo.class);
            flag = productSpecValueInnerServiceSMOImpl.updateProductSpecValue(productSpecValuePo);
            if (flag < 1) {
                throw new CmdException("修改价格失败");
            }
        }

    }
}
